package Client.Manager
{
	import Client.VO.*;
	import Client.ValueDefine.*;
	
	/**
	 * class		AreaMgr
	 * @desc		used to manage the areas
	 * @author		Aqua
	 * @date		2011-9-6
	 */
	public class AreaMgr
	{
		//---------------------//
		//      Singleton      //
		//---------------------//
		
		/**************************************************************************
		 * function				instance getter
		 * @desc				a private function used to enforce singlton
		 * @return	AreaMgr		singleton instance of this class
		 *************************************************************************/
		public static function get instance():AreaMgr
		{
			if(null == _instance)
			{
				_instance = new AreaMgr(singleton);
			}
		
			return _instance;
		}
		
		/**************************************************************************
		 * function			singleton
		 * @desc			a private function used to enforce singlton
		 *************************************************************************/
		private static function singleton():void
		{
			
		}
		
		
		//---------------------//
		//     Constructor     //
		//---------------------//
		
		/**************************************************************************
		 * function			AreaMgr
		 * @desc			constructor of class
		 * @param	s		a private function used to enforce singlton
		 *************************************************************************/
		public function AreaMgr(s:Function = null):void
		{
			if (singleton != s)
			{
				throw new Error("This is a singleton class!");
			}
		}
		
		
		//-----------------//
		//     Publics     //
		//-----------------//
		
		/**************************************************************************
		 * function					loadArea
		 * @desc					load infomation of all areas in a specified map
		 * @param	xml				xml file that specifies areas' IDs
		 *************************************************************************/
		public function loadArea(xml:XML):void
		{
			var dataXML:XML;
			
			dataXML = ResourceMgr.instance.getResource(ResourceMgr.XML_AREA) as XML;
			
			loadLand(xml.lands, dataXML.lands.land);
			loadSea(xml.sea, dataXML.seas.sea);
			
			if (GameMgr.instance.globalData.portEnabled)
			{
				loadPort(xml.port, dataXML.ports.port);
			}
		}
		
		/**************************************************************************
		 * function				setArmy
		 * @desc				set an army in a specified area
		 * @param	areaID		unique id of the area
		 * @param	armyVO		ArmyVO of the army which to be set
		 *************************************************************************/
		public function setArmy(areaID:String, armyVO:ArmyVO):void
		{
			var vo:*;
			
			vo = searchAreaByID(areaID);
			
			if (null == vo)
			{
				// log
				return;
			}
			
			vo.army = armyVO;
		}
		
		//------------------//
		//     Privates     //
		//------------------//
		
		/**************************************************************************
		 * function				loadLand
		 * @desc				load lands' data from xml and save to VOs
		 * @param	idList		a list that contains IDs of lands which appear in
		 * 						this map
		 * @param	dataList	a list that contains data of all lands
		 *************************************************************************/
		private function loadLand(idList:XMLList, dataList:XMLList):void
		{
			var item	:XML;
			var vo		:LandVO;
			
			landList = {};
			
			for each(item in idList)
			{
				vo					= new LandVO();
				vo.id				= item.id;
				vo.name				= item.name;
				vo.building			= item.building;
				vo.numCrown			= item.numCrown;
				vo.numSupply		= item.numSupply;
				vo.hometown			= item.hometown;
				vo.controller		= HouseName.NULL;
				vo.powerToken		= false;
				vo.army				= null;
				_landList[vo.id]	= vo;
			}
		}
		
		/**************************************************************************
		 * function				loadSea
		 * @desc				load seas' data from xml and save to VOs
		 * @param	idList		a list that contains IDs of seas which appear in
		 * 						this map
		 * @param	dataList	a list that contains data of all seas
		 *************************************************************************/
		private function loadSea(idList:XMLList, dataList:XMLList):void
		{
			var item	:XML;
			var vo		:LandVO;
			
			seaList = {};
			
			for each(item in idList)
			{
				vo				= new LandVO();
				vo.id			= item.id;
				vo.name			= item.name;
				vo.controller	= HouseName.NULL;
				vo.army			= null;
				_seaList[vo.id]	= vo;
			}
		}
		
		/**************************************************************************
		 * function			searchAreaByID
		 * @desc			search an area by its id
		 * @param	id		unique id of the area
		 * @return	*		LandVO or SeaVO or PortVO
		 *************************************************************************/
		private function searchAreaByID(id:String):*
		{
			var vo		:*;
			var lists	:Array;
			var i		:int;
			
			lists = [_landList, _seaList, _portList];
			i = 0;
			
			while (i < 3)
			{
				vo = lists[i][id];
				
				if (null != vo)
				{
					break;
				}
				
				i ++;
			}
			
			return vo;
		}
		
		
		private static var _instance:AreaMgr;
		
		private var _landList	:Object;
		private var _seaList	:Object;
		private var _portList	:Object;
	}
	
}